package com.javaEE.gamepigeon.util;

import org.apache.commons.io.FileUtils;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Component
public class FastDFSUtils {

    private static Logger logger = LoggerFactory.getLogger(FastDFSUtils.class);

    static {
        try {
            File tempFile = new File("temp/fdfs_client.conf");
            FileUtils.copyToFile(new ClassPathResource("fdfs_client.conf").getInputStream(),tempFile);
            String filePath = tempFile.getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String[] upLoad(MultipartFile file){
        String name = file.getOriginalFilename();
        String[] uploadResults = null;
        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();
            uploadResults = storageClient.upload_file(file.getBytes(),name.substring(name.lastIndexOf(".")+1),null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null==uploadResults){
            logger.error("上传失败");
            return uploadResults;
        }
        return uploadResults;
    }

    public static FileInfo getFileInfo(String groupName,String remoteFileName){
        StorageClient storageClient = null ;
        try {
            storageClient = getStorageClient();
            FileInfo fileInfo = storageClient.get_file_info(groupName,remoteFileName);
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static InputStream downFile(String groupName,String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient= getStorageClient();
            byte[] fileByte = storageClient.download_file(groupName,remoteFileName);
            InputStream inputStream = new ByteArrayInputStream(fileByte);
            return inputStream;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Async
    public void deleteFile(String groupName,String remoteFileName){
        StorageClient storageClient = null ;
        try {
            storageClient = getStorageClient();
            logger.info("正在删除"+remoteFileName);
            int result = storageClient.delete_file(groupName,remoteFileName);
            logger.info(String.valueOf(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getTrackerUrl(){
//        TrackerClient trackerClient = new TrackerClient();
//        TrackerServer trackerServer;
//        StorageServer storeStorage =null;
//        try {
//            trackerServer = trackerClient.getTrackerServer();
//            storeStorage = trackerClient.getStoreStorage(trackerServer);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return "https://www.671666.cn:443/";
    }


    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = new StorageClient(trackerServer,null);
        return storageClient;

    }

    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        return trackerServer;
    }

}
